package com.surfacing.rcp.client.modelManager;

import java.util.Calendar;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

import com.surfacing.rcp.core.net.NetWrapper;
import com.surfacing.rcp.model.QueryFilter;
import com.surfacing.rcp.model.codec.NfsDecoder;
import com.surfacing.rcp.model.entity.Anforderung;
import com.surfacing.rcp.model.entity.Dienststelle;
import com.surfacing.rcp.model.entity.Doctor;
import com.surfacing.rcp.model.entity.Einsatzart;
import com.surfacing.rcp.model.entity.Journal;
import com.surfacing.rcp.model.entity.Login;
import com.surfacing.rcp.model.entity.Naca;
import com.surfacing.rcp.model.entity.Nfs;
import com.surfacing.rcp.model.entity.ProtokollNr;
import com.surfacing.rcp.model.entity.StatAnforderung;
import com.surfacing.rcp.model.entity.StatDienststelle;
import com.surfacing.rcp.model.entity.StatDoctor;
import com.surfacing.rcp.model.entity.StatEinsatzart;
import com.surfacing.rcp.model.entity.StatNaca;
import com.surfacing.rcp.model.entity.StatNfs;
import com.surfacing.rcp.model.entity.StatTransportziel;
import com.surfacing.rcp.model.entity.Transportziel;
import com.surfacing.rcp.model.entity.User;
import com.surfacing.rcp.model.util.MyUtils;

/**
 * The model factory manages the lifecyle of all model managers. Furthermore it
 * provides methods to query the data from the server.
 * 
 * @author Michael
 */
public class ModelFactory {
	// the shared instance
	private static ModelFactory instance;

	// the model manager to handle
	private final JournalManager journalManager = new JournalManager();
	private final NfsManager nfsManager = new NfsManager();
	private final DoctorManager doctorManager = new DoctorManager();
	private final EinsatzartManager einsatzartManager = new EinsatzartManager();
	private final TransportzielManager transportzielManager = new TransportzielManager();
	private final DienststelleManager dienststelleManager = new DienststelleManager();
	private final AnforderungManager anforderungManager = new AnforderungManager();
	private final NacaManager nacaManager = new NacaManager();
	private final UserManager userManager = new UserManager();
	private final LoginManager loginManager = new LoginManager();
	private final StatNacaManager statNacaManager = new StatNacaManager();
	private final StatEinsatzartManager statEinsatzartManager = new StatEinsatzartManager();
	private final StatTransportzielManager statTransportzielManager = new StatTransportzielManager();
	private final StatDienststelleManager statDienststelleManager = new StatDienststelleManager();
	private final StatAnforderungManager statAnforderungManager = new StatAnforderungManager();
	private final StatDoctorManager statDoctorManager = new StatDoctorManager();
	private final StatNfsManager statNfsManager = new StatNfsManager();
	private final NextProtokollNrManager protokollNrManager = new NextProtokollNrManager();

	// private final StaffManager staffManager = new StaffManager();
	// private final LoginManager loginManager = new LoginManager()

	/**
	 * Private class constructor.
	 */
	private ModelFactory() {
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static ModelFactory getInstance() {
		if (instance == null)
			instance = new ModelFactory();
		return instance;
	}

	/**
	 * Queries the server sequentially for all needed data.
	 */
	public void initalizeModel() {
		// get the client connection
		org.eclipse.core.runtime.jobs.Job job = new org.eclipse.core.runtime.jobs.Job(
				"Request data listing") {
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				NetWrapper net = NetWrapper.getDefault();
//				String now = MyUtils.timestampToString(Calendar.getInstance()
//						.getTimeInMillis(), MyUtils.timeAndDateFormat);
				// Set up a filter for the current day
				// QueryFilter dateFilter = new QueryFilter();
				// dateFilter.add(IFilterTypes.DATE_FILTER, now);
				net.requestListing(Journal.ID, null);
				net.requestListing(Nfs.ID, null);
				net.requestListing(Doctor.ID, null);
				net.requestListing(Anforderung.ID, null);
				net.requestListing(Dienststelle.ID, null);
				net.requestListing(Einsatzart.ID, null);
				net.requestListing(Naca.ID, null);
				net.requestListing(Transportziel.ID, null);
				net.requestListing(User.ID, null);
				net.requestListing(Login.ID, null);
				net.requestListing(StatAnforderung.ID, null);
				net.requestListing(StatDienststelle.ID, null);
				net.requestListing(StatDoctor.ID, null);
				net.requestListing(StatEinsatzart.ID, null);
				net.requestListing(StatNfs.ID, null);
				net.requestListing(StatTransportziel.ID, null);
				net.requestListing(StatNaca.ID, null);
				net.requestListing(ProtokollNr.ID, null);
				return Status.OK_STATUS;
			}
		};
		job.setPriority(org.eclipse.core.runtime.jobs.Job.SHORT);
		job.setUser(true);
		job.schedule();
		
	}
	
	public JournalManager getJournalManager() {
		return journalManager;
	}

	public NfsManager getNfsManager() {
		return nfsManager;
	}

	public DoctorManager getDoctorManager() {
		return doctorManager;
	}

	public EinsatzartManager getEinsatzartManager() {
		return einsatzartManager;
	}

	public TransportzielManager getTransportzielManager() {
		return transportzielManager;
	}

	public DienststelleManager getDienststelleManager() {
		return dienststelleManager;
	}

	public AnforderungManager getAnforderungManager() {
		return anforderungManager;
	}

	public NacaManager getNacaManager() {
		return nacaManager;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public LoginManager getLoginManager() {
		return loginManager;
	}

	public StatNacaManager getStatNacaManager() {
		return statNacaManager;
	}

	public StatEinsatzartManager getStatEinsatzartManager() {
		return statEinsatzartManager;
	}

	public StatTransportzielManager getStatTransportzielManager() {
		return statTransportzielManager;
	}

	public StatDienststelleManager getStatDienststelleManager() {
		return statDienststelleManager;
	}

	public StatAnforderungManager getStatAnforderungManager() {
		return statAnforderungManager;
	}

	public StatDoctorManager getStatDoctorManager() {
		return statDoctorManager;
	}

	public StatNfsManager getStatNfsManager() {
		return statNfsManager;
	}

	public NextProtokollNrManager getNextProtokollManager() {
		return protokollNrManager;
	}
	
}
